<?php
class Admin_customer_popup_Controller extends Template_Controller {
	
	public $mr = array();
	public $search;
	public $template = 'admin_popup/index';
	
    public function __construct()
    {
        parent::__construct();
        
		$this->search = array(
			'type' => '',
			'name' => '',
			'phone' => '',
			'address' => '',		
		);
    }
    
	public function __call($method, $arguments)
	{
		// Disable auto-rendering
		$this->auto_render = FALSE;
	}
	
	function _get_submit()
	{
		if($this->session->get('error_msg'))
			$this->template->error_msg = $this->session->get('error_msg');
		if($this->session->get('warning_msg'))
			$this->template->warning_msg = $this->session->get('warning_msg');
		if($this->session->get('success_msg'))
			$this->template->success_msg = $this->session->get('success_msg');
		if($this->session->get('info_msg'))
			$this->template->info_msg = $this->session->get('info_msg');
		if($this->session->get('frm'))		
			$this->template->content->mr = $this->session->get('frm');
	}
	
    public function index()
    {
		$this->showlist();
    }
    
    public function billing()
    {
    	$this->mr['type'] = 'billing';
		$this->showlist();
    }
    
    public function shipping()
    {
    	$this->mr['type'] = 'shipping';
		$this->showlist();
    }
    
   	public function showlist()
	{
		$this->template->content = new View('admin_customer/search_popup');
		
		//Assign
        if($this->search['type'])
			$this->mr['type'] = $this->search['type'];
		if($this->search['name'])
			$this->template->content->name = $this->search['name'];
		if($this->search['phone'])
			$this->template->content->phone = $this->search['phone'];
		if($this->search['address'])
			$this->template->content->address = $this->search['address'];
		
		$model = new Customer_Model();	
		$this->where_sql();	
		$mlist = $model->get_with_active();
		
		$this->pagination = new Pagination(array(
    		'base_url'    => 'admin_proposal/search/',
		    'uri_segment'    => 'page',
		    'total_items'    => count($mlist),
		    'items_per_page' => $this->site['site_num_line2'],
		    'style'          => 'digg',
		));	
		$this->db->limit($this->pagination->items_per_page,$this->pagination->sql_offset);	
		
		$this->where_sql();
		$mlist = $model->get_with_active();
		$this->template->content->mlist = $mlist;
		$this->template->content->mr = $this->mr;	
	}
   
   private function where_sql()
   {
		if($this->search['name']){
			$this->db->like('customers_firstname',$this->search['name']);
			$this->db->orlike(array('customers_lastname' => $this->search['name']));  
		}
		if($this->search['phone']){
			$this->db->orlike(array('customers_phone' => $this->search['phone']));
		}
		if($this->search['address']){
			$this->db->orlike(array('customers_address' => $this->search['address']));	
		}
	}
	
	public function search()
	{
		//Get    	
    	if($this->session->get('sess_search')){			
			$this->search = $this->session->get('sess_search');
		}
		$type = $this->input->post('hd_type');
		if(isset($type)){
			$this->search['type'] = $type;
		}
		$name = $this->input->post('txt_name');
		if(isset($name)){
			$this->search['name'] = $name;
		}
		$phone = $this->input->post('txt_phone');
		if(isset($phone)){
			$this->search['phone'] = $phone;
		}
		$address = $this->input->post('txt_address');
		if(isset($address)){
			$this->search['address'] = $address;
		}
		
		//Set
		$this->session->set_flash('sess_search',$this->search);

		$this->showlist();	
	}
}
?>